import { _decorator, Component, Node, Sprite, UITransform, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('LoadingProgress')
export class LoadingProgress extends Component {

    @property(Node)
    progressNode: Node = null;
    @property(Sprite)
    mask: Sprite = null;

    @property
    clip_speed: number = 1;

    private startPos: Vec3 = new Vec3();
    init(): void {
        this.progressNode.getPosition(this.startPos);

        this.progress = 0;
    }

    protected update(dt: number): void {
        let x = this.mask.getComponent(UITransform).width * this._progress;
        if (this.progressNode.position.x > x) {
            this.progressNode.setPosition(0, 0, 0);
        }

        let current = this.progressNode.position;
        this.progressNode.setPosition(current.x + this.clip_speed, 0, 0);
    }

    private _progress: number = 0
    set progress(value: number) {
        this._progress = value;
        this.mask.fillRange = this._progress;
    }

    get progress(): number {
        return this._progress;
    }
}


